Extracting and Visualizing Stock Data

Description

Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some stock data, you will then display this data in a graph.

Table of Contents

  • Define a Function that Makes a Graph
  • Question 1: Use yfinance to Extract Stock Data
  • Question 2: Use Webscraping to Extract Tesla Revenue Data
  • Question 3: Use yfinance to Extract Stock Data
  • Question 4: Use Webscraping to Extract GME Revenue Data
  • Question 5: Plot Tesla Stock Graph
  • Question 6: Plot GameStop Stock Graph

Estimated Time Needed: 30 min


In [ ]:
In [1]:

Define Graphing Function

In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.

In [47]:

Question 1: Use yfinance to Extract Stock Data

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.

In [48]:

Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.

In [49]:
Out[49]:
Date Open High Low Close Volume Dividends Stock Splits
0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 281494500 0.0 0.0
1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 257806500 0.0 0.0
2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 123282000 0.0 0.0
3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 77097000 0.0 0.0
4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 103003500 0.0 0.0

Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.

In [ ]:

Question 2: Use Webscraping to Extract Tesla Revenue Data

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm Save the text of the response as a variable named html_data.

In [7]:

Parse the html data using beautiful_soup.

In [31]:
Out[31]:
[<title>Tesla Revenue 2010-2022 | TSLA | MacroTrends</title>]
In [62]:

Using BeautifulSoup or the read_html function extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.

Click here if you need help locating the table

Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab

soup.find_all("tbody")[1]

If you want to use the read_html function the table is located at index 1

Execute the following line to remove the comma and dollar sign from the Revenue column.

Execute the following lines to remove an null or empty strings in the Revenue column.

In [63]:

Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.

In [64]:
Out[64]:
Date Revenue
48 2010-09-30 31
49 2010-06-30 28
50 2010-03-31 21
52 2009-09-30 46
53 2009-06-30 27

Question 3: Use yfinance to Extract Stock Data

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.

In [35]:

Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.

In [36]:

Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.

In [37]:
Out[37]:
Date Open High Low Close Volume Dividends Stock Splits
0 2002-02-13 00:00:00-05:00 1.620128 1.693350 1.603296 1.691667 76216000 0.0 0.0
1 2002-02-14 00:00:00-05:00 1.712707 1.716074 1.670626 1.683250 11021600 0.0 0.0
2 2002-02-15 00:00:00-05:00 1.683250 1.687458 1.658002 1.674834 8389600 0.0 0.0
3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 0.0 0.0
4 2002-02-20 00:00:00-05:00 1.615920 1.662210 1.603296 1.662210 6892800 0.0 0.0

Question 4: Use Webscraping to Extract GME Revenue Data

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html. Save the text of the response as a variable named html_data.

In [55]:

Parse the html data using beautiful_soup.

In [59]:
Out[59]:
[<title>GameStop Revenue 2006-2020 | GME | MacroTrends</title>]

Using BeautifulSoup or the read_html function extract the table with GameStop Quarterly Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.

Click here if you need help locating the table

Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab

soup.find_all("tbody")[1]

If you want to use the read_html function the table is located at index 1

In [60]:

Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.

In [61]:
Out[61]:
Date Revenue
57 2006-01-31 1667
58 2005-10-31 534
59 2005-07-31 416
60 2005-04-30 475
61 2005-01-31 709

Question 5: Plot Tesla Stock Graph

Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla'). Note the graph will only show data upto June 2021.

In [65]:
05010015020025030020102012201420162018202002k4k6k8k10k
TeslaDatePrice ($US)Revenue ($US Millions)Historical Share PriceHistorical RevenueDate

Question 6: Plot GameStop Stock Graph

Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.

In [44]:
020406080200420062008201020122014201620182020500100015002000250030003500
GameStopDatePrice ($US)Revenue ($US Millions)Historical Share PriceHistorical RevenueDate

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Azim Hirjani

Change Log

Date (YYYY-MM-DD) Version Changed By Change Description
2022-02-28 1.2 Lakshmi Holla Changed the URL of GameStop
2020-11-10 1.1 Malika Singla Deleted the Optional part
2020-08-27 1.0 Malika Singla Added lab to GitLab

© IBM Corporation 2020. All rights reserved.